package org.example.exam.service.impl;

import io.minio.errors.*;
import org.example.exam.entity.Banner;
import org.example.exam.mapper.BannerMapper;
import org.example.exam.service.BannerService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.exam.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    // 将文件服务调进来
    @Autowired
    private FileUploadService fileUploadService;


    @Override
    public String uploadBannerImage(MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        // 1、文件非空校验
        if (file.isEmpty()) {
            // 因为我们有全局异常处理器，只要有一异常就不会返回到正确的结果中了
            // 会直接到我们的全局异常处理器中，再给用户返回提示
            throw new RuntimeException("上传的轮播图文件为空！上传失败！");
        }

        // 2、判断文件的类型（与上传的视频做区分）
        String type = file.getContentType();
        if (ObjectUtils.isEmpty(type) || !type.startsWith("image")) {
            throw new RuntimeException("上传的轮播图文件类型错误！上传失败！");
        }

        // 3、调用文件上传业务
        String url = fileUploadService.uploadFile("banner", file);

        // 4、回显地址
        return url;
    }
}